package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.bestvike.linq.Linq;
import com.ruoyi.system.common.RandomListUtil;
import com.ruoyi.system.domain.CacheTool;
import com.ruoyi.system.domain.WordBean;
import com.ruoyi.system.domain.WordSearchParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BmWordMapper;
import com.ruoyi.system.domain.BmWord;
import com.ruoyi.system.service.IBmWordService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-01
 */
@Service
public class BmWordServiceImpl implements IBmWordService 
{
    @Autowired
    private BmWordMapper bmWordMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public BmWord selectBmWordById(Long id)
    {
        return bmWordMapper.selectBmWordById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param bmWord 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BmWord> selectBmWordList(BmWord bmWord)
    {
        return bmWordMapper.selectBmWordList(bmWord);
    }

    @Override
    public WordBean getWordsList(WordSearchParams wordSearchParams)
    {
        WordBean paperDataList = new WordBean();
        //版本，年级，学期
        List<BmWord> where = Linq.of(CacheTool.wordList).where(p -> p.getVersionCode() ==wordSearchParams.getVersionCode()
        && p.getGradeCode()==wordSearchParams.getGradeCode() && p.getTermCode()==wordSearchParams.getTermCode()).toList();
        //写字表、词语表
        if(wordSearchParams.getWordScope()==1)
        {
            where = Linq.of(where).where(p->p.getWordType()==1).toList();

        }
        if(wordSearchParams.getWordScope()==2)
        {
           where = Linq.of(where).where(p->p.getWordType()==2).toList();
        }

        //按照课文，单元取并集
        List<BmWord> courstFilterList = Linq.of(where).where(p -> wordSearchParams.getWordCourses().contains(p.getCourseCode())).toList();
        List<BmWord> unitFilterList = Linq.of(where).where(p -> wordSearchParams.getWordUnits().contains(p.getUnitCode())).toList();
        List<BmWord> unionList = new ArrayList<>();
        for(BmWord item:courstFilterList)
        {
            unionList.add(item);
        }

        for(BmWord item:unitFilterList){
            if(Linq.of(unionList).where(p->p.getId()==item.getId()).toList().size() == 0)
            {
                unionList.add(item);
            }
        }
        if(wordSearchParams.getWordType()==2){
            //随机试卷
            if(wordSearchParams.getWordCount() < unionList.size())
            {
                unionList = new RandomListUtil<BmWord>(unionList).getRandomList(wordSearchParams.getWordCount().intValue());

            }

        }
        unionList = Linq.of(unionList).orderBy(p->p.getCourseCode()).thenBy(p->p.getWordSort()).toList();
        List<List<String>> wordLists = new ArrayList<>();
        List<String> wordList = new ArrayList<>();
        List<List<String>> pinyinLists = new ArrayList<>();
        List<String> pinyinList = new ArrayList<>();
        for(BmWord item:unionList)
        {
            wordList = new ArrayList<>();
            pinyinList = new ArrayList<>();
            if(item.getWordName().length()>1){
                String words = item.getWordName();
                String[] pinyins = item.getWordPinyin().split(";");

                for (int i = 0; i < item.getWordName().length(); i++) {
                    wordList.add(words.substring(i,1));
                    pinyinList.add(pinyins[i]);
                }
                wordLists.add(wordList);
                pinyinLists.add(pinyinList);
            }
            else
            {
                wordList.add(item.getWordName());
                pinyinList.add(item.getWordPinyin());
                wordLists.add(wordList);
                pinyinLists.add(pinyinList);

            }

        }
        paperDataList.setWordList(wordLists);
        paperDataList.setPinyinList(pinyinLists);
        return paperDataList;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param bmWord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBmWord(BmWord bmWord)
    {
        return bmWordMapper.insertBmWord(bmWord);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param bmWord 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBmWord(BmWord bmWord)
    {
        return bmWordMapper.updateBmWord(bmWord);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBmWordByIds(Long[] ids)
    {
        return bmWordMapper.deleteBmWordByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBmWordById(Long id)
    {
        return bmWordMapper.deleteBmWordById(id);
    }
}
